#!/usr/bin/env python
#
#   Copyright (C) 2006 Jonathan Saggau                                     
#   saggau@gmail.com                                                       
#                                                                          
#   This program is free software; you can redistribute it and/or modify  
#   it under the terms of the GNU General Public License as published by  
#   the Free Software Foundation; either version 2 of the License, or     
#   (at your option) any later version.                                    
#                                                                          
#   This program is distributed in the hope that it will be useful,        
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
#   GNU General Public License for more details.                          
#                                                                          
#   You should have received a copy of the GNU General Public License     
#   along with this program; if not, write to the                         
#   Free Software Foundation, Inc.,                                        
#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  
#

import objc
from Foundation import *
from AppKit import *
# starts up some debugging stuff

from PyObjCTools import NibClassBuilder
from trigger import controller as primitiveModel
import buffer

NibClassBuilder.extractClasses("MainMenu")

class Model(NibClassBuilder.AutoBaseClass):
    """big pyobjc wrapper class of getters and setters for the cross plat 
    model in trigger.  Makes it KVC complient for cocoa."""

    def init(self):
        raise Exception, "cannot init with init.  \
                          Use initWithController_NodeID_Buffer_AppDelegate"
        
    def initWithController_NodeID_AppDelegate(self, controller, 
                                              nodeID, appDelegate):
        buffer = buffer.Buffer.initWithController_BufferID_FilePath_(\
                                                     controller, None, None)
        self = self.initWithController_NodeID_Buffer_AppDelegate(controller, \
                                                nodeID, buffer, appDelegate)
        return self
        
    def initWithController_NodeID_Buffer_AppDelegate(self, controller, nodeID,
                                                     buffer, appDelegate):
        self = super(Model, self).init() 
        if self is None: return None
        self._buffer = buffer
        self._primitiveModel = primitiveModel.Controller(controller, \
                                                nodeID,
                                                buffer.ID())
        self._appDelegate = appDelegate
        return self

    def setBuffer_(self, buffer):
        self._buffer = buffer
        self._primitiveModel.setBufferID(buffer.ID())

    def buffer(self):
        return self._buffer

    def bufferID(self):
        return self._buffer.ID

    def bufferDuration(self):
        "returns duration in seconds"
        bufferInfo = self._buffer.query()
        frames = float(bufferInfo['numberofFrames'])
        sampleRate = float(bufferInfo['sampleRate'])
        try:
            return (frames/sampleRate)
        except ZeroDivisionError:
            return 0

    def channels(self):
        bufferInfo = self._buffer.query()
        return bufferInfo['numberOfChannels']
        #will be zero if there isn't one

    def isNotMono(self):
        return self.channels() == 1
        
    def setSoundFile_(self, filePath):
        self._appDelegate.setSoundFile_ForTrigger_(str(filePath), self)
        #figure out how to get the delegate

    def soundFile(self):
        return self._buffer.filePath()
        
    def setBufferPosition_(self, position):
        framePosition = int(position * self._buffer.query()["sampleRate"])
        self._primitiveModel.setBufferPosition(framePosition)
        
    def bufferPosition(self):
        return self._primitiveModel.bufferPosition
        
    def close(self):
        self._primitiveModel.close()
        
    def setAttackSharpness_(self, sharpnessFloat):
        self._primitiveModel.setAttackSharpness(float(sharpnessFloat))
        
    def attackSharpness(self):
        return self._primitiveModel.attackSharpness
        
    def setSynthType_(self, type, channel):
        self._primitiveModel.setSynthType(str(type), int(channel))
        
    def synthType(self):
        return self._primitiveModel.synthType
        
    def setInput_(self, inputInt):
        self._primitiveModel.setInput(int(inputInt))
        
    def input(self):
        return self._primitiveModel.input
        
    def setTriggerID_(self, trigIDInt):
        self._primitiveModel.setTriggerID(int(trigIDInt))
        
    def triggerID(self):
        return self._primitiveModel.triggerID
        
    def setEnvCurve_(self, curveInt):
        self._primitiveModel.setEnvCurve(int(trigIDInt))
        
    def envCurve(self):
        return self._primitiveModel.envCurve
        
    def setPan_(self, panFloat):
        self._primitiveModel.setPan(float(panFloat))
        
    def pan(self):
        return self._primitiveModel.pan
        
    def setVolume_(self, volFloat):
        self._primitiveModel.setVolume(float(volFloat))
        
    def volume(self):
        return self._primitiveModel.volume
        
    def setSampleDuration_(self, duration):
        self._primitiveModel.setSampleDuration(duration)
        
    def sampleDuration(self):
        return self._primitiveModel.sampleDuration
        
    def setSensitivity_(self, sensitivityFloat):
        self._primitiveModel.setSensitivity(float(sensitivityFloat))
        
    def sensitivity(self):
        return self._primitiveModel.sensitivity
        
    def setOverlap_(self, overlapInt):
        self._primitiveModel.setOverlap(overlapInt)
        
    def overlap(self):
        return self._primitiveModel.overlap
        
    def setEnvType_(self, envType):
        self._primitiveModel.setEnvType(str(envType))
        
    def envType(self):
        return self._primitiveModel.envType
        
    def envTypes(self):
        return self._primitiveModel.envTypes    
        
    def setSynthType_(self, type):
        self._primitiveModel.setSynthType(type)
        
    def synthType(self):
        return self._primitiveModel.synthType
        #Current synthType
        
    def synthTypes(self):
        return self._primitiveModel.synthTypes

    def nodeID(self):
        return self._primitiveModel.nodeID
        
    def setSendToClient_(self, boolVal):
        self._primitiveModel.sendToClient(bool(boolVal))
        
    def sendToClient(self):
        return self._primitiveModel.sendToClient
        
    def sendingToClient(self):
        return self._primitiveModel.sendingToClient()
        
    def setClientPort_(self, port):
        self._primitiveModel.setClientPort(port)
        
    def clientPort(self):
        return model.clientPort
        
    def setClientIP_(self, IP):
        self._primitiveModel.setClientIP(IP)
        
    def clientIP(self):
        return self._primitiveModel.clientIP
